package cn.stylefeng.guns.modular.gridsystem.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.context.login.LoginContext;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.gridsystem.entity.*;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizGatewayResult;
import cn.stylefeng.guns.modular.gridsystem.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizExceptionLogParam;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 故障明细表 控制器
 *
 * @author: ssy
 * @date: 2021/09/14 11:47
 */
@RestController
public class BizExceptionLogController {

    @Resource
    private BizExceptionLogService bizExceptionLogService;

    @Resource
    private BizExceptionService bizExceptionService;

    /**
     * 分页查询故障明细表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizExceptionLog/page")
    @BusinessLog(title = "故障明细表_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BizExceptionLogParam bizExceptionLogParam) {
        return new SuccessResponseData(bizExceptionLogService.page(bizExceptionLogParam));

//        BizException exception = bizExceptionService.getById(exceptionId);
//        if(exception!=null){
//            Integer deviceId = exception.getDeviceId();
//            QueryWrapper<BizGatewayChannel> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("device_id",deviceId);
//            List<BizGatewayChannel> list = bizGatewayChannelService.list(queryWrapper);
//            Set<String> set = new HashSet<>();
//            for (BizGatewayChannel bizGatewayChannel:list){
//                String channel = bizGatewayChannel.getChannel();
//                set.add(channel);
//            }
//            return new SuccessResponseData(bizExceptionLogService.page(bizExceptionLogParam,set));
//        }
//        return new ErrorResponseData(500,"获取故障信息失败!");

    }

    @GetMapping("/bizExceptionLog/pageByAtAndEId")
    public ResponseData page(Integer exceptionId,String createdAt) {
        BizExceptionLogParam bizExceptionLogParam = new BizExceptionLogParam();
        bizExceptionLogParam.setExceptionId(exceptionId);
        bizExceptionLogParam.setCreatedAt(createdAt);
        return new SuccessResponseData(bizExceptionLogService.pageList(bizExceptionLogParam));
    }

    /**
     * 获取全部故障明细表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizExceptionLog/list")
    @BusinessLog(title = "故障明细表_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BizExceptionLogParam bizExceptionLogParam) {
        return new SuccessResponseData(bizExceptionLogService.list(bizExceptionLogParam));
    }

    /**
     * 查看详情故障明细表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizExceptionLog/detail")
    @BusinessLog(title = "故障明细表_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BizExceptionLogParam.detail.class) BizExceptionLogParam bizExceptionLogParam) {
        return new SuccessResponseData(bizExceptionLogService.detail(bizExceptionLogParam));
    }

    /**
     * 添加故障明细表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizExceptionLog/add")
    @BusinessLog(title = "故障明细表_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BizExceptionLogParam.add.class) BizExceptionLogParam bizExceptionLogParam) {
        bizExceptionLogService.add(bizExceptionLogParam);
        return new SuccessResponseData();
    }

    /**
     * 删除故障明细表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizExceptionLog/delete")
    @BusinessLog(title = "故障明细表_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BizExceptionLogParam.delete.class) BizExceptionLogParam bizExceptionLogParam) {
        //获取用户信息
        final LoginContext me = LoginContextHolder.me();
        //校验用户角色，管理员才能删除
        if (!me.isSuperAdmin()) {
            return new ErrorResponseData(403, "无权操作，只有管理员才能删除");
        }
        bizExceptionLogService.delete(bizExceptionLogParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑故障明细表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizExceptionLog/edit")
    @BusinessLog(title = "故障明细表_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BizExceptionLogParam.edit.class) BizExceptionLogParam bizExceptionLogParam) {
        bizExceptionLogService.edit(bizExceptionLogParam);
        return new SuccessResponseData();
    }


    /**
     *获取设备对应通道最新故障信息
     * @param gatewayCode
     * @param gatewayChannel
     * @return
     */
    @GetMapping("/bizExceptionLog/getChannelNewfaultFile")
    public ResponseData getChannelNewfaultFile(String gatewayCode,String gatewayChannel) {
        Map<String, Object> map = bizExceptionLogService.getChannelNewfaultFile(gatewayCode,gatewayChannel);
        return new SuccessResponseData(map);
    }

    @GetMapping("/bizExceptionLog/getExceptionLog")
    public ResponseData getExceptionLog(Integer deviceId) {
        QueryWrapper<BizException> wrapper = new QueryWrapper<>();
        wrapper.eq("device_id",deviceId);
        List<BizException> bizExceptions = bizExceptionService.list(wrapper);
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < bizExceptions.size(); i++) {
            Integer id = bizExceptions.get(i).getId();
            set.add(id);
        }
        BizExceptionLogParam exceptionLogParam = new BizExceptionLogParam();
        List<Map<String, Object>> page = bizExceptionLogService.thirdList(exceptionLogParam,set,deviceId);
        return ResponseData.success(page);
    }

}
